package com.cjb.cloudsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.TraceabilityData;
import com.cjb.cloudsystem.mapper.TraceabilityDataMapper;
import com.cjb.cloudsystem.service.TraceabilityDataService;
import com.cjb.cloudsystem.vo.TraceabilityDataVO;
import com.cjb.common.exception.ServerException;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.util.Common;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class TraceabilityDatasServiceImpl
 * @Date 2023-07-27 21:41
 */
@Service
@AllArgsConstructor
public class TraceabilityDataServiceImpl extends BaseRepositoryImpl<TraceabilityDataMapper, TraceabilityData> implements TraceabilityDataService {
    /**
     * 日志
     */
    private final static Logger log = LoggerFactory.getLogger(TraceabilityDataServiceImpl.class);
    /*
     * 注入mapper层
     */
    private final TraceabilityDataMapper traceabilityDataMapper;

    /**
     * 单体保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public boolean save(TraceabilityData traceabilityData) {
        if (Common.isEmpty(traceabilityData)) {
            throw new ServerException("sysObject is empty");
        }
        baseMapper.insert(traceabilityData);
        return true;
    }

    /**
     * 批量保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Integer listSave(List<TraceabilityData> traceabilityData) {
        return baseMapper.insertBatchSomeColumn(traceabilityData);
    }

    /**
     * 批量更新
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listUpdate(List<TraceabilityData> traceabilityData) {
        saveOrUpdateBatch(traceabilityData, traceabilityData.size());
    }

    /**
     * 批量删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listDelete(List<TraceabilityData> traceabilityData) {
        List<Long> collect = traceabilityData.stream().map(TraceabilityData::getId)
                .distinct()
                .collect(Collectors.toList());
        traceabilityDataMapper.deleteBatchIds(collect);
    }

    /**
     * 通过id删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public int deleteById(TraceabilityData traceabilityData) {
        return traceabilityDataMapper.deleteById(traceabilityData.getId());
    }

    /**
     * 单个查询
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public TraceabilityData queryById(TraceabilityData traceabilityData) {
        return traceabilityDataMapper.selectById(traceabilityData.getId());
    }

    /**
     * 分页
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Page queryPage(Page traceabilityDataPage, TraceabilityDataVO traceabilityDataVO) {
        return traceabilityDataMapper.selectPage(traceabilityDataPage, getWrapper(traceabilityDataVO));
    }

    /**
     * 查询属性
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private LambdaQueryWrapper<TraceabilityData> getWrapper(TraceabilityDataVO query) {
        LambdaQueryWrapper<TraceabilityData> wrapper = Wrappers.lambdaQuery();
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(TraceabilityData::getId, query.getId());
        }
        if (Common.isNotNull(query.getDeviceId())) {
            wrapper.eq(TraceabilityData::getDeviceId, query.getDeviceId());
        }
        if (Common.isNotNull(query.getUserId())) {
            wrapper.eq(TraceabilityData::getUserId, query.getUserId());
        }
        if (Common.isNotNull(query.getRemark())){
            wrapper.eq(TraceabilityData::getRemark, query.getRemark());
        }
        if (Common.isNotNull(query.getIsTrue())) {
            wrapper.eq(TraceabilityData::getIsTrue, query.getIsTrue());
        }
        if (Common.isNotNull(query.getEncrySecret())) {
            wrapper.eq(TraceabilityData::getEncrySecret, query.getEncrySecret());
        }
        if (Common.isNotNull(query.getEncryAlg())) {
            wrapper.eq(TraceabilityData::getEncryAlg, query.getEncryAlg());
        }
        if (Common.isNotNull(query.getIsDel())) {
            wrapper.eq(TraceabilityData::getIsDel, query.getIsDel());
        }
        if (Common.isNotNull(query.getUpdater())) {
            wrapper.eq(TraceabilityData::getUpdater, query.getUpdater());
        }
        if (Common.isNotNull(query.getCreateTime())) {
            wrapper.eq(TraceabilityData::getCreateTime, query.getCreateTime());
        }
        if (Common.isNotNull(query.getUpdateTime())) {
            wrapper.eq(TraceabilityData::getUpdateTime, query.getUpdateTime());
        }
        wrapper.orderByDesc(TraceabilityData::getId);
        return wrapper;
    }


    /**
     * 返回全部信息
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<TraceabilityData> returnAllData() {
        return traceabilityDataMapper.selectList(null);
    }

}